package com.shopRedis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
public class RedisController {
    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @RequestMapping("opsForValue/get.api")
    public String opsForValueGet(String key){

        return this.redisTemplate.opsForValue().get(key);
    }
    @RequestMapping("opsForValue/set.api")
    public boolean opsForvalueSet(String key,String value,Integer time){


        if(time != null){

             this.redisTemplate.opsForValue().set(key,value,time,TimeUnit.MINUTES);
            return true;
        }
        this.redisTemplate.opsForValue().set(key,value);
        return true;
    }
    @RequestMapping("opsForValue/append.api")
    public long opsForvalueAppend(String key,String value){

        return this.redisTemplate.opsForValue().append(key,value);
    }
    @RequestMapping("opsForValue/getByKeys.api")
    public List<String> multiGet(List<String> list){


        return this.redisTemplate.opsForValue().multiGet(list);
    }
    @RequestMapping("opsForValue/getAndSet.api")
    public String  getAndSet(String key,String value){

        return this.redisTemplate.opsForValue().getAndSet(key,value);
    }

    @RequestMapping("opsForValue/getStringLength.api")
    public Long  getStringLength(String key){

        return this.redisTemplate.opsForValue().size(key);
    }

    @RequestMapping("deleteKey.api")
    public boolean  deleteKey(String key){
        this.redisTemplate.delete(key);
        return true;
    }

}
